import request from '../util/request';
const delay = (millisecond) => {
    return new Promise((resolve) => {
        setTimeout(resolve, millisecond);
    });
};

export default {
    namespace: 'cards',
    state: {
        cardsList: []
    },

    effects: {
        *queryList(_, sagaEffects){
            const listData = [{
                name: '张三',
                desc: '家住上海普陀',
                url: 'https://cdn.nlark.com/yuque/0/2018/jpeg/130359/1535469793817-avatar/588e3aa5-362b-4277-97ec-96bf85de561d.jpeg?x-oss-process=image/resize,m_fill,w_64,h_64/format,png'
            },
            {
                name: '李四',
                desc: '家住湖北襄阳',
                url: 'https://gw.alipayobjects.com/zos/rmsportal/wYnHWSXDmBhiEmuwXsym.png?x-oss-process=image/resize,m_fill,w_64,h_64/format,png'
            }];
            const { call, put } = sagaEffects;
            yield call(delay, 2000);
            yield put({type: 'initList', payload: listData});
        },
        *addOne(_, sagaEffects){
            const { call, put } = sagaEffects;
            const listData = [{
                name: '王五',
                desc: '家住上海普陀',
                url: 'https://cdn.nlark.com/yuque/0/2018/jpeg/130359/1535469793817-avatar/588e3aa5-362b-4277-97ec-96bf85de561d.jpeg?x-oss-process=image/resize,m_fill,w_64,h_64/format,png'
            }]
            yield call(delay, 2000);
            yield put({type: 'addList', payload: listData});
        }
    },
    reducers: {
        initList(state, { payload }) {
            const cardsList = [...payload];
            return {
                cardsList
            };
        },
        addList(state, { payload }) {
            const newCardsData = [...payload];
            const nextData = state.cardsList.concat(newCardsData);
            return {
                cardsList: nextData
            }
        }
    }
};